//
//  PGViewController.m
//  PhotographicGuide
//
//  Created by indream on 13-2-6.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGTableViewController.h"
#import "PGDetailViewController.h"
#import "PGDeviceTableSource.h"
#import "PGArticleManager.h"
#import "CKRefresh/CKRefreshControl.h"
#import "PGArticleManager.h"
#import "PGArticle.h"
#import "PGWebViewController.h"
#import <QuartzCore/QuartzCore.h>
static PGTableViewController*_instance;

static NSString* NOTIFICATION_TABLE_REFRESH = @"__NOTIFICATION_TABLE_REFRESH__";

@interface PGTableViewController ()

@end

@implementation PGTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:[PGTableViewController sharedController] action:@selector(doRefresh:) forControlEvents:UIControlEventValueChanged];
    [self doRefresh:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadTable:) name:NOTIFICATION_TABLE_REFRESH object:nil];
}
-(void)viewDidUnload{
    [super viewDidUnload];
    [self setMoreCell:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:NOTIFICATION_TABLE_REFRESH object:nil];
}
- (void)doRefresh:(CKRefreshControl *)sender {
    [[PGArticleManager sharedManager]updateLastestArticles];
}
-(void)reloadTable:(NSNotification*)notification{
    NSLog(@"count:%d",[PGArticleManager sharedManager].list.count);
    [self.refreshControl endRefreshing];
    [self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if([PGArticleManager sharedManager].list){
        return [PGArticleManager sharedManager].list.count+1;
    }
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if([PGArticleManager sharedManager].list){
        if(indexPath.row<[PGArticleManager sharedManager].list.count){
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
                return 236;
            }else{
                return 491;
            }
        }else{
            return 54.0;
        }
    }return 54.0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if([PGArticleManager sharedManager].list&&indexPath.row<[PGArticleManager sharedManager].list.count){
        PGViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PGViewCell"];
        if (cell == nil) {
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
                [[NSBundle mainBundle] loadNibNamed:@"PGViewCell" owner:self options:nil];
            }else{
                [[NSBundle mainBundle] loadNibNamed:@"PGViewCell_iPad" owner:self options:nil];
            }
            cell = _tvCell;
            self.tvCell = nil;
        }
        cell.iconView.image = nil;
        cell.thumbView.image = [UIImage imageNamed:@"home_default_image.png"];
        [cell loadImage:[(PGArticle*)[[PGArticleManager sharedManager].list objectAtIndex:indexPath.row] imageSmall]];
        if([(PGArticle*)[[PGArticleManager sharedManager].list objectAtIndex:indexPath.row] icon]){
            [cell loadIcon:[(PGArticle*)[[PGArticleManager sharedManager].list objectAtIndex:indexPath.row] icon]];
        }
        cell.titleLabel.text = [(PGArticle*)[[PGArticleManager sharedManager].list objectAtIndex:indexPath.row] name];
        [cell updateDate:[[(PGArticle*)[[PGArticleManager sharedManager].list objectAtIndex:indexPath.row] time]intValue]];

        return cell;
    }else{
        PGMoreTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"PGMoreTableViewCell"];
        if(cell==nil){
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
                [[NSBundle mainBundle] loadNibNamed:@"PGMoreTableViewCell" owner:self options:nil];
            }else{
                [[NSBundle mainBundle] loadNibNamed:@"PGMoreTableViewCell_iPad" owner:self options:nil];
            }
            cell = _moreCell;
            self.moreCell = nil;
        }
        if([PGArticleManager sharedManager].isEndArticle){
            
            [cell.activityIndicator stopAnimating];
            cell.moreLabel.text = @"每天持续更新，请继续关注";
        }else{
            if([PGArticleManager sharedManager].isDownload){
                [cell.activityIndicator startAnimating];
                cell.moreLabel.text = @"正在加载中...";
            }else{
                [cell.activityIndicator stopAnimating];
                cell.moreLabel.text = @"每天持续更新，点击查看更多";
            }
        }
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if([PGArticleManager sharedManager].list&&indexPath.row<[PGArticleManager sharedManager].list.count){
        PGArticle *article = (PGArticle*)[[PGArticleManager sharedManager].list objectAtIndex:indexPath.row];
        NSString *url = article.url;
        if([url componentsSeparatedByString:@"http://"].count>1){
            PGWebViewController *controller = [[PGWebViewController alloc]init];
            controller.url = url;
            controller.title = article.name;
            [self.navigationController pushViewController:controller animated:YES];
        }else{
            PGDetailViewController *controller = [[PGDetailViewController alloc]initWithNibName:[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?@"PGDetailViewController":@"PGDetailViewController_iPad" bundle:nil];
            controller.articleId = [article.articleid intValue];
            [self.navigationController pushViewController:controller animated:YES];
        }
    }else{
        
        if([PGArticleManager sharedManager].isEndArticle){
            PGMoreTableViewCell *cell = (PGMoreTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
            [cell.activityIndicator stopAnimating];
            cell.moreLabel.text = @"每天持续更新，请继续关注";
        }else{
            PGMoreTableViewCell *cell = (PGMoreTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
            if(cell){
                [cell.activityIndicator startAnimating];
                cell.moreLabel.text = @"正在加载中...";
            }
            [[PGArticleManager sharedManager]updateOlderArticles];
        }
    }
}
-(void)doFinishLoadMore{
    [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
}
+(PGTableViewController*)sharedController{
    if(_instance==nil){
        _instance = [[PGTableViewController alloc]init];
    }
    return _instance;
}

@end
